package com.hluobo.library.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hluobo.library.dao.impl.BasicDao;
import com.hluobo.library.service.BorrowLogService;
import com.hluobo.library.vo.BorrowVo;

@Component("borrowLogService")
public class BorrowLogServiceImpl extends BasicDao implements BorrowLogService {

	@Override
	public List<BorrowVo> query(int userId) {
		String queryString = "select blog.id as logId,blog.borrow_id as userId,"+
						"book.name as name,blog.back_dt as backDate, book.img_url as url "+
				"From BorrowLog blog,Book book where blog.book_id = book.id and blog.borrow_id=?";
		List<BorrowVo> borrowLogs = new ArrayList<BorrowVo>();
		List res = em.createQuery(queryString).setParameter(1, userId).getResultList();
		Iterator it = res.iterator();
		while (it.hasNext()) {
			Object[] ob = (Object[]) it.next();
			int logId = (Integer) ob[0];
			int uId = (Integer) ob[1];
			String name = (String) ob[2];
			Date backDt = (Timestamp) ob[3];
			String url = (String) ob[4];
			String backDate = "";
			if(backDt != null){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				backDate = sdf.format(backDt);
			}

			BorrowVo bv = new BorrowVo(logId, uId, name, backDate, url);
			borrowLogs.add(bv);
		}
		return borrowLogs;
	}

}
